﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfSamples
{
    /// <summary>
    /// CellAnimationWindow.xaml 的交互逻辑
    /// </summary>
    public partial class CellAnimationWindow : Window
    {
        public CellAnimationWindow()
        {
            InitializeComponent();
        }       

        private void OnGenerateButton_Click(object sender, RoutedEventArgs e)
        {
            var viewModel = DataContext as CellAnimationViewModel;
            if (viewModel != null)
            {
                viewModel.Stop();
            }
            DataContext = new CellAnimationViewModel();
        }

        private void OnMoveButton_Click(object sender, RoutedEventArgs e)
        {
            var viewModel = DataContext as CellAnimationViewModel;
            if (viewModel != null)
            {
                viewModel.Update();
            }
        }

        private void LastNameCM_Click(object sender, RoutedEventArgs e)
        {

        }
    }

    public class CellAnimationViewModel : INotifyPropertyChanged
    {
        private IEnumerable<Employee> _employees;
        private CancellationTokenSource _cancelSource;
        public event PropertyChangedEventHandler PropertyChanged;

        public CellAnimationViewModel()
        {
            EmployeeInfoDataSource = GenerateEmployees(100);
        }

        public IEnumerable<Employee> EmployeeInfoDataSource
        {
            get
            {
                return _employees;
            }
            private set
            {
                _employees = value;
                OnPropertyChanged(nameof(EmployeeInfoDataSource));
            }
        }

        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        private IEnumerable<Employee> GenerateEmployees(int count)
        {
            var employees = new List<Employee>();
            return Enumerable.Range(1, count).Select(index => new Employee { FirstName = "u" + index, EmployeeNumber=index.ToString(), Salary=index*1000, LastName="S"+index}).ToList();
        }

        internal void Update()
        {
            if (_cancelSource == null || _cancelSource.IsCancellationRequested)
            {
                _cancelSource = new CancellationTokenSource();
            }
            if (EmployeeInfoDataSource == null || !EmployeeInfoDataSource.Any())
            {
                return;
            }

            Task.Factory.StartNew(() =>
            {
                var employees = EmployeeInfoDataSource;
                while (_cancelSource != null && !_cancelSource.IsCancellationRequested)
                {
                    var random = new Random();
                    var e = employees.ElementAt(random.Next() % employees.Count());
                    e.Salary = Math.Round(random.NextDouble() * 10000, 0);
                    e.LastName = "S" + random.Next()% employees.Count();
                    Thread.Sleep(50);
                }
            }, _cancelSource.Token);
        }

        internal void Stop()
        {
            if (_cancelSource != null && !_cancelSource.IsCancellationRequested)
            {
                _cancelSource.Cancel();
            }
        }
    }

    public class Employee : INotifyPropertyChanged
    {
        private string _firstName;
        private string _lastName;
        private string _employeeNumber;
        private double _salary;

        public event PropertyChangedEventHandler PropertyChanged;


        public string FirstName
        {
            get
            {
                return _firstName;
            }
            set
            {
                if (_firstName != value)
                {
                    _firstName = value;
                    OnPropertyChanged(nameof(FirstName));
                }
            }
        }

        public string LastName
        {
            get
            {
                return _lastName;
            }
            set
            {
                if (_lastName != value)
                {
                    _lastName = value;
                    OnPropertyChanged(nameof(LastName));
                }
            }
        }

        public string EmployeeNumber
        {
            get
            {
                return _employeeNumber;
            }
            set
            {
                if (_employeeNumber != value)
                {
                    _employeeNumber = value;
                    OnPropertyChanged(nameof(EmployeeNumber));
                }
            }
        }

        public double Salary
        {
            get
            {
                return _salary;
            }
            set
            {
                if (_salary != value)
                {
                    _salary = value;
                    OnPropertyChanged(nameof(Salary));
                }
            }
        }


        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
